#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project  : ecshop
# @File     : backend_add_luckymoney.py
# @Author   : WanLiWei
# @Time     : 2021/7/3 0:31
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("http://192.168.4.138/ecshop/admin/privilege.php?act=login")
driver.find_element(By.NAME, "username").send_keys("admin")
driver.find_element(By.NAME, "password").send_keys("wlw12345678")
driver.find_element(By.CLASS_NAME, "button").click()

# 进入id为menu-frame的frame
driver.switch_to.frame('menu-frame')
# 点击促销管理
driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[2]').click()
# 点击红包类型
driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[2]/ul/li[2]/a').click()
# 退出frame
driver.switch_to.default_content()

# 进入id为main-frame的frame
driver.switch_to.frame('main-frame')
driver.find_element_by_xpath('/html/body/h1/span[1]/a').click()
# 类型名称
driver.find_element(By.NAME,'type_name').send_keys("红包来了")
# 红包金额
driver.find_element(By.NAME,'type_money').send_keys(100)
# 最小订单金额
driver.find_element(By.ID,'min_goods_amount').send_keys(100)
# 如何发放此类型红包
sleep(2)
driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[2]').click()
# 发放起始日期
# 删除元素中属性的方法--日期处理
# 开始日期
js = "document.getElementById('send_start_date').removeAttribute('ReadOnly')"
driver.execute_script(js)
driver.find_element_by_id("send_start_date").clear()
driver.find_element_by_id("send_start_date").send_keys("2021-11-11")
# 发放结束日期
js = "document.getElementById('send_end_date').removeAttribute('ReadOnly')"
driver.execute_script(js)
driver.find_element_by_id("send_end_date").clear()
driver.find_element_by_id("send_end_date").send_keys("2022-02-22")
# 使用起始日期
js = "document.getElementById('use_start_date').removeAttribute('ReadOnly')"
driver.execute_script(js)
driver.find_element_by_id("use_start_date").clear()
driver.find_element_by_id("use_start_date").send_keys("2022-11-11")
# 使用结束日期
js = "document.getElementById('use_end_date').removeAttribute('ReadOnly')"
driver.execute_script(js)
driver.find_element_by_id("use_end_date").clear()
driver.find_element_by_id("use_end_date").send_keys("2022-12-22")
# 确定按钮
sleep(2)
driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[10]/td[2]/input[1]').click()
# 退出frame
driver.switch_to.default_content()

